﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FireBillBook.Models;
using FireBillBook.Core.Models;
using FireBillBook.Core.Dtos;
using FireBillBook.Core.Services;
using FireBillBook.Core.Models.QueryContexts;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System.IO;


namespace FireBillBook.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class BillingController : ControllerBase
    {
        private IBillingService _billing;
        private ILogger _logger;

        public BillingController(IBillingService billing, ILogger<BillingController> logger)
        {
            _billing = billing;
            _logger = logger;
        }

        [HttpGet("[action]/{id}")]
        public async Task<CommonResponseModel<ConsumptionRecordDto>> GetDetail(int id)
        {
            try
            {
                var dto = await _billing.GetByIdAsync(id);
                return new CommonResponseModel<ConsumptionRecordDto>(dto)
                {
                    IsSuccess = true
                };
            }
            catch (Exception ex)
            {
                return new CommonResponseModel<ConsumptionRecordDto>(null)
                {
                    IsSuccess = false,
                    Code = -1,
                    Message = ex.Message
                };
            }
        }

        [HttpPost("[action]")]
        [ProducesResponseType(typeof(CommonResponseModel<PagingModel<ConsumptionRecordDto>>), 200)]
        [ProducesErrorResponseType(typeof(BaseReponseModel))]
        public async Task<BaseReponseModel> List(ConsumptionRecordListQueryContext queryContext)
        {
            try
            {
                var result = await _billing.ListAsync(queryContext);

                return new CommonResponseModel<PagingModel<ConsumptionRecordDto>>(result)
                {
                    IsSuccess = true,
                };
            }
            catch (Exception ex)
            {
                return new BaseReponseModel()
                {
                    IsSuccess = false,
                    Code = -1,
                    Message = ex.Message
                };
            }
        }

        // POST api/<BillingController>
        [HttpPost("[action]")]
        public async Task<BaseReponseModel> Save([FromBody] ConsumptionRecordDto dto)
        {
            try
            {
                await _billing.SaveAsync(dto);

                return new BaseReponseModel()
                {
                    IsSuccess = true,
                };
            }
            catch (Exception ex)
            {
                return new BaseReponseModel()
                {
                    IsSuccess = false,
                    Code = -1,
                    Message = ex.Message
                };
            }
        }

        [HttpPost("[action]/{id}")]
        public async Task<BaseReponseModel> Delete(int id)
        {
            try
            {
                await _billing.DeleteAsync(id);
                return new BaseReponseModel()
                {
                    IsSuccess = true
                };
            }
            catch (Exception ex)
            {
                return new BaseReponseModel()
                {
                    IsSuccess = false,
                    Code = -1,
                    Message = ex.Message
                };
            }
        }

    }
}
